package com.lanlian.cayman.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.lanlian.cayman.LLApplication;
import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.BankSelectorWindow;
import com.lanlian.cayman.view.CitySelectorWindow;
import com.lanlian.commonlib.base.BaseFragment;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.CityInfo;
import com.lanlian.commonlib.bean.PreRegisterInfo;
import com.lanlian.commonlib.bean.ProvinceInfo;
import com.lanlian.commonlib.bean.ReqLazyEnum;
import com.lanlian.commonlib.bean.ReqVerifyBank;
import com.lanlian.commonlib.bean.RespLazyEnum;
import com.lanlian.commonlib.bean.UtilEnumInfo;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.utillib.WorksSizeCheckUtil;

import static com.lanlian.cayman.activity.OpenAccentActivity.respPreRegister;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 14:18
 *     desc   : 注册开户第三步，账户信息
 *     version: 1.0
 * </pre>
 */
@SuppressLint("ValidFragment")
public class RegisterTwoFragment extends BaseFragment implements View.OnClickListener {

    private EditText mEtBankCard;
    private TextView mTvBankType;
    private TextView mTvBankCity;
    private EditText mEtBankBranch;
    private TextView mTvNext;
    private TextView mTvPrevious;

    private boolean mIsEditHasContent;
    private boolean mIsFirst = true;

    private CitySelectorWindow mCitySelectorWindow;
    private BankSelectorWindow mBankSelectorWindow;

    private OnPreviousClickListener mOnPreviousClickListener;

    public interface OnPreviousClickListener {
        void onNextClick();

        void onPreviousClick();
    }

    public RegisterTwoFragment(OnPreviousClickListener onPreviousClickListener) {
        this.mOnPreviousClickListener = onPreviousClickListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_register_two, null);
        initView(view);
        setListeners();
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mEtBankCard = view.findViewById(R.id.et_bank_card);
        mTvBankType = view.findViewById(R.id.tv_bank_type);
        mTvBankCity = view.findViewById(R.id.tv_bank_city);
        mEtBankBranch = view.findViewById(R.id.et_bank_branch);
        mTvNext = view.findViewById(R.id.tv_next);
        mTvPrevious = view.findViewById(R.id.tv_previous);
        mCitySelectorWindow = new CitySelectorWindow(getActivity());
        mCitySelectorWindow.setOnCityChooseListener(new CitySelectorWindow.OnCityChooseListener() {
            @Override
            public void onCityChoose(final ProvinceInfo provinceInfo, final CityInfo cityInfo) {
                mTvBankCity.setText(String.format("%s %s", provinceInfo.getName(), cityInfo.getName()));
                LLApplication.reqUserRegister.setBankCity(cityInfo.getId());
                LLApplication.reqUserRegister.setBankProvince(provinceInfo.getId());
            }
        });
        mBankSelectorWindow = new BankSelectorWindow(getActivity());
        mBankSelectorWindow.setOnBankChooseListener(new BankSelectorWindow.OnBankChooseListener() {
            @Override
            public void onBankChoose(UtilEnumInfo bank) {
                mTvBankType.setText(bank.getName());
                LLApplication.reqUserRegister.setBankType(bank.getId());
            }
        });
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvBankType.setOnClickListener(this);
        mTvBankCity.setOnClickListener(this);
        mTvNext.setOnClickListener(this);
        mTvPrevious.setOnClickListener(this);
        WorksSizeCheckUtil worksSizeCheckUtil = new WorksSizeCheckUtil(mTvNext);
        worksSizeCheckUtil.addAllEditText(mEtBankBranch, mEtBankCard, mTvBankCity, mTvBankType);
        worksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                mIsEditHasContent = isHasContent;
                LLApplication.reqUserRegister.setBankCard(mEtBankCard.getText().toString());
                LLApplication.reqUserRegister.setBankBranch(mEtBankBranch.getText().toString());
                checkData();
            }
        });
    }

    public void setPreRegisterInfo() {
        if (respPreRegister != null && mIsFirst) {
            mIsFirst = false;
            PreRegisterInfo preRegisterInfo = respPreRegister.getData();
            mEtBankCard.setText(preRegisterInfo.getBankCard());
            mEtBankBranch.setText(preRegisterInfo.getBankBranch());
            mBankSelectorWindow.setBankId(preRegisterInfo.getBankType());
            mCitySelectorWindow.setCityId(preRegisterInfo.getBankProvince(),
                    preRegisterInfo.getBankCity());

            LLApplication.reqUserRegister.setBankCity(preRegisterInfo.getBankCity());
            LLApplication.reqUserRegister.setBankProvince(preRegisterInfo.getBankProvince());
            LLApplication.reqUserRegister.setBankType(preRegisterInfo.getBankType());
            LLApplication.reqUserRegister.setBankBranch(preRegisterInfo.getBankBranch());
            LLApplication.reqUserRegister.setBankCard(preRegisterInfo.getBankCard());

            if (!TextUtils.isEmpty(preRegisterInfo.getBankType()) ||
                    !TextUtils.isEmpty(preRegisterInfo.getBankType()) ||
                    !TextUtils.isEmpty(preRegisterInfo.getBankType())) {
                VolleyRequestUtils.getInstance().getLazyEnum(this.toString(),
                        true, true, new ReqLazyEnum(), new INetWorkCallback() {
                            @Override
                            public void onSuccess(Object response) {
                                RespLazyEnum respUtilEnum = (RespLazyEnum) response;
                                mTvBankType.setText(respUtilEnum.getBankType().get(preRegisterInfo.getBankType()));
                                if (!TextUtils.isEmpty(preRegisterInfo.getBankProvince())) {
                                    mTvBankCity.setText(String.format("%s %s",
                                            respUtilEnum.getProvince().get(preRegisterInfo.getBankProvince()),
                                            respUtilEnum.getCity().get(preRegisterInfo.getBankCity())));
                                }
                            }

                            @Override
                            public void onFail(BaseResponse baseResponse) {
                            }
                        });
            }
        }
    }

    private void checkData() {
        if (mIsEditHasContent) {
            mTvNext.setEnabled(true);
            mTvNext.setBackgroundResource(R.mipmap.login_btn);
        } else {
            mTvNext.setEnabled(false);
            mTvNext.setBackgroundResource(R.drawable.bg_click_false);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_next:
                ReqVerifyBank reqVerifyBank = new ReqVerifyBank();
                reqVerifyBank.setCardNo(LLApplication.reqUserRegister.getBankCard());
                VolleyRequestUtils.getInstance().verifyBankCard(this.toString(), reqVerifyBank, new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        mOnPreviousClickListener.onNextClick();
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        showToast(baseResponse.getMsg());
                    }
                });
                break;
            case R.id.tv_bank_city:
                if (mCitySelectorWindow == null) {
                    mCitySelectorWindow = new CitySelectorWindow(getActivity());
                    mCitySelectorWindow.setOnCityChooseListener(new CitySelectorWindow.OnCityChooseListener() {
                        @Override
                        public void onCityChoose(final ProvinceInfo provinceInfo, final CityInfo cityInfo) {
                            mTvBankCity.setText(String.format("%s %s", provinceInfo.getName(), cityInfo.getName()));
                            LLApplication.reqUserRegister.setBankCity(cityInfo.getId());
                            LLApplication.reqUserRegister.setBankProvince(provinceInfo.getId());
                        }
                    });
                }
                mCitySelectorWindow.showDialog();
                break;
            case R.id.tv_bank_type:
                if (mBankSelectorWindow == null) {
                    mBankSelectorWindow = new BankSelectorWindow(getActivity());
                    mBankSelectorWindow.setOnBankChooseListener(new BankSelectorWindow.OnBankChooseListener() {
                        @Override
                        public void onBankChoose(UtilEnumInfo bank) {
                            mTvBankType.setText(bank.getName());
                            LLApplication.reqUserRegister.setBankType(bank.getId());
                        }
                    });
                }
                mBankSelectorWindow.showDialog();
                break;
            case R.id.tv_previous:
                mOnPreviousClickListener.onPreviousClick();
                break;
            default:
                break;
        }
    }
}
